<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class User extends Model{

	/**
	* The attributes that are mass assignable.
	*
	* @var array
	*/
	protected $fillable = [
		'user_name', 'email', 'password', 'thumb',
	];

	/**
	 * 关联到模型的数据表
	 *
	 * @var string
	 */
	protected $table = 'dqm_user';

	/**
	 * The attributes that should be hidden for arrays.
	 *
	 * @var array
	 */
	protected $hidden = [
		'password', 'remember_token',
	];

	/**
	 * 字段：用户唯一标识
	 */
	const FIELD_USER_ID = 'user_id';

	/**
	 * 字段：用户邮箱账号
	 */
	const FIELD_EMAIL = 'email';

	/**
	 * 字段：用户名，可重名
	 */
	const FIELD_USER_NAME = 'user_name';

	/**
	 * 字段：椒盐值，随机六位数字
	 */
	const FIELD_SALT_WORD = 'salt_word';

	/**
	 * 字段：登陆密码
	 */
	const FIELD_PASSWORD = 'password';

	/**
	 * 字段：vip用户，0：不是，1：是
	 */
	const FIELD_VIP_STATUS = 'vip_status';

	/**
	 * 字段：邮箱激活状态，0：未激活，1：已激活
	 */
	const FIELD_EMAIL_STATUS = 'email_status';

	/**
	 * 字段：用户状态，0：禁用，1：正常
	 */
	const FIELD_USER_STATUS = 'user_status';

	/**
	 * 字段：后台管理员，0：不是，1：是
	 */
	const FIELD_IS_ADMIN = 'is_admin';

	/**
	 * 字段：用户邮箱账号
	 */
	const FIELD_THUMB = 'thumb';

	/**
	 * 字段：创建时间
	 */
	const FIELD_CREATED_TIME = 'created_time';

	/**
	 * 字段：更新时间
	 */
	const FIELD_UPDATED_TIME = 'updated_time';

	/**
	 * 用户状态: 1、可用 ; 0、禁用/鎖定
	 */
	const ENABLE_USER_STATUS = 1;
	const DISABLE_USER_STATUS = 0;

	/**
	 * 邮箱状态: 1、可用 ; 0、禁用/鎖定
	 */
	const ENABLE_EMAIL_STATUS = 1;
	const DISABLE_EMAIL_STATUS = 0;

	/**
	 * 新增用户
	 *
	 * @param array $data
	 * @return bool
	 */
	static public function add($data = array()){
		if(!empty($data)){
			$now = date('Y-m-d', time());
			if(!isset($data[self::FIELD_CREATED_TIME])){
				$data[self::FIELD_CREATED_TIME] = $now;
			}
			if(!isset($data[self::FIELD_UPDATED_TIME])) {
				$data[self::FIELD_UPDATED_TIME] = $now;
			}
			$user_id = User::insertGetId($data);
			if($user_id){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}

	/**
	 * 设置随机字符 椒盐值
	 *
	 * @param int $len
	 * @param null $chars
	 * @return string
	 */
	static public function setSalt($len = 6, $chars = null){
		if (is_null($chars)) {
			$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		}
		mt_srand(10000000*(double)microtime());
		for ($i = 0, $salt = '', $lc = strlen($chars)-1; $i < $len; $i++) {
			$salt .= $chars[mt_rand(0, $lc)];
		}
		return $salt;
	}

	/**
	 * 加密密码
	 *
	 * @param $password
	 * @param $salt
	 * @return string
	 */
	static public function encryptPassword($password, $salt){
		return md5(md5($password).$salt);
	}

	/**
	 * 一对多关系 获取当前用户下的所有笔记
	 *
	 * @return \Illuminate\Database\Eloquent\Relations\HasMany
	 */
	 public function bijis(){
		return $this->hasMany('App\Biji');
	}

	/**
	 * 根据用户ID获取指定用户
	 *
	 * @param array $conditions
	 * @param array $select_field
	 * @return mixed
	 */
	static public function getUserByUserId($conditions = [], $select_field = []){
	 	$result = self::getNormalUserByConditions($conditions, $select_field);
		return $result;
	}

	/**
	 * 根据查询条件获取用户个数
	 *
	 * @param array $conditions
	 * @return mixed
	 */
	static public function getUserCountByConditions($conditions = []){
		$result = User::where($conditions['where'])->count();
		return $result;
	}

	/**
	 * 根据条件更新账号信息
	 *
	 * @param array $update_data
	 * @param array $conditions
	 * @return bool
	 */
	static public function updateUserByConditions($update_data = [], $conditions = []){
		$result = User::where($conditions['where'])->update($update_data);
		if($result){
			return true;
		}
		return false;
	}

	/**
	 * 根据查询条件获取指定用户信息
	 *
	 * @param array $conditions
	 * @param array $select_fields
	 * @return mixed
	 */
	static public function getNormalUserByConditions($conditions = [], $select_fields = []){
	 	$conditions['where'][self::FIELD_USER_STATUS] = self::ENABLE_USER_STATUS;
	 	$conditions['where'][self::FIELD_EMAIL_STATUS] = self::ENABLE_EMAIL_STATUS;
		$result = User::select($select_fields)->where($conditions['where'])->first();
		return $result;
	}
}
